package com.netty.cn.model.socket.client.core;

import java.time.LocalDateTime;
import java.util.Scanner;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
 
public class MyClientHandler extends SimpleChannelInboundHandler<String> {
 
 
 
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(ctx.channel().remoteAddress());
        System.out.println("client output:" + msg);
        
        System.out.println("请输入信息");
        Scanner scan=new Scanner(System.in);//控制台输入控件
        String message=scan.next();
        
        ctx.writeAndFlush("from client:" + LocalDateTime.now()+" "+message);
    }
 
    /**
     * 初始连接发送消息
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.channel().writeAndFlush("来自客户端的问候！");
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.channel().close();
    }
}